//
//  JsonConverterSettingVC.swift
//  SFDevFly
//
//  Created by shen on 2023/10/8.
//

import Cocoa

class JsonConverterSettingVC: NSViewController {
    @IBOutlet var prefixKeyLab: NSTextField!
    @IBOutlet var prefixField: NSTextField!

    @IBOutlet var parentClassKeyLab: NSTextField!
    @IBOutlet var parentClassField: NSTextField!

    @IBOutlet var keyMapperKeyLab: NSTextField!
    @IBOutlet var keyMapperSwitch: NSSwitch!

    @IBOutlet var autoHumpKeyLab: NSTextField!
    @IBOutlet var autoHumpSwitch: NSSwitch!

    @IBOutlet var customHeaderKeyLab: NSTextField!
    @IBOutlet var customHeaderSwitch: NSSwitch!

    @IBOutlet var headerKeyLab: NSTextField!
    @IBOutlet var headerField: NSTextField!

    @IBOutlet var saveBtn: NSButton!

    @IBOutlet var backgroundView: NSColorView!

    var fileConfigChangedClosure: (() -> ())?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        updateConfigUI()
    }

    func setupUI() {
        title = "parameter_setting_title".localized
        backgroundView.backgroundColor = NSColor(named: "LineColor")
        prefixKeyLab.stringValue = "parameter_classes_prefix".localized
        parentClassKeyLab.stringValue = "parameter_parent_class_title".localized
        customHeaderKeyLab.stringValue = "parameter_custom_file_header_title".localized
        autoHumpKeyLab.stringValue = "parameter_auto_case_underline_hump".localized
        headerKeyLab.stringValue = "parameter_file_header_title".localized
        keyMapperKeyLab.stringValue = "parameter_key_mapper_title".localized
        saveBtn.title = "parameter_save_title".localized

        headerField.textColor = NSColor(hex: 0x3ab54a)
        headerField.font = NSFont(name: "Menlo", size: 14)
    }

    func updateConfigUI() {
        prefixField.stringValue = kCacheManager.file.prefix ?? ""
        parentClassField.stringValue = kCacheManager.file.parentName ?? ""
        autoHumpSwitch.state = kCacheManager.file.autoCaseUnderline ? .on : .off
        customHeaderSwitch.state = kCacheManager.file.isCustomHeader ? .on : .off
        keyMapperSwitch.state = kCacheManager.file.useKeyMapper ? .on : .off
        headerField.isEditable = customHeaderSwitch.state == .on
        headerField.stringValue = kCacheManager.file.header ?? ""
    }

    @IBAction func saveConfigAction(_ sender: NSButton) {
        kCacheManager.file.prefix = prefixField.stringValue
        kCacheManager.file.parentName = parentClassField.stringValue
        kCacheManager.file.header = headerField.stringValue
        kCacheManager.file.isCustomHeader = customHeaderSwitch.state.rawValue == 1
        kCacheManager.file.autoCaseUnderline = autoHumpSwitch.state.rawValue == 1
        kCacheManager.file.useKeyMapper = keyMapperSwitch.state.rawValue == 1
        kCacheManager.asyncConfigSave()
        fileConfigChangedClosure?()
        dismiss(nil)
    }

    @IBAction func customFileHeaderSwitch(_ sender: NSSwitch) {
        kCacheManager.file.isCustomHeader = customHeaderSwitch.state.rawValue == 1
        kCacheManager.file.autoCaseUnderline = autoHumpSwitch.state.rawValue == 1
        kCacheManager.asyncConfigSave()
        updateConfigUI()
    }
}
